package com.blockchain.console.service.user.achieve;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import com.blockchain.common.enums.CertificateType;
import com.blockchain.common.enums.ChargeStatus;
import com.blockchain.common.enums.ChargeType;
import com.blockchain.common.enums.IsPass;
import com.blockchain.common.enums.WithdrawStatus;
import com.blockchain.common.enums.XlbType;
import com.blockchain.console.config.ConsoleConst;
import com.blockchain.console.service.financial.entity.CzglRecord;
import com.blockchain.console.service.financial.entity.TxglRecord;
import com.blockchain.console.service.financial.query.CzglRecordQuery;
import com.blockchain.console.service.user.UserManage;
import com.blockchain.console.service.user.entity.User;
import com.blockchain.console.service.user.entity.ZhxxEntity;
import com.blockchain.console.service.user.query.UserQuery;
import com.blockchain.framework.config.ConfigureProvider;
import com.blockchain.framework.data.sql.SQLConnectionProvider;
import com.blockchain.framework.service.ServiceFactory;
import com.blockchain.framework.service.ServiceResource;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.framework.service.query.ArrayParser;
import com.blockchain.framework.service.query.ItemParser;
import com.blockchain.framework.service.query.Paging;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.EmailUtil;
import com.blockchain.util.StringHelper;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.variables.P2PConst;
import com.blockchain.variables.defines.SystemVariable;

public class UserManageImpl extends AbstractUserService implements UserManage {

	public static class UserManageFactory implements ServiceFactory<UserManage> {

		@Override
		public UserManage newInstance(ServiceResource serviceResource) {
			return new UserManageImpl(serviceResource);
		}
	}

	public UserManageImpl(ServiceResource serviceResource) {
		super(serviceResource);
	}

	protected static final String USER_SQL = "SELECT T6010.F01 userid, T6010.F02 username,T6010.F04 iphone,T6010.F06 sfsd,"
			+ "	T6011.F02 name,T6011.F03 zjlx,T6011.F04 zjhm,T6011.F09 zcsj,T6011.F13 sftggjrz  FROM"
			+ " T6010 LEFT JOIN T6011 ON T6010.F01=T6011.F01 ";

	protected static final ArrayParser<User> USER_PARSER = new ArrayParser<User>() {
		@Override
		public User[] parse(ResultSet resultSet) throws SQLException {
			ArrayList<User> list = null;
			while (resultSet.next()) {
				User user = new User();
				user.userId = resultSet.getInt(1);
				user.userName = resultSet.getString(2);
				user.iphone = resultSet.getString(3);
				user.userState = EnumParser.parse(IsPass.class, resultSet.getString(4));
				user.name = resultSet.getString(5);
				user.certificateType = EnumParser.parse(CertificateType.class, resultSet.getString(6));
				user.zjhm = resultSet.getString(7);
				user.registrationTime = resultSet.getTimestamp(8);
				user.sftggjrz = EnumParser.parse(IsPass.class, resultSet.getString(9));
				if (list == null) {
					list = new ArrayList<>();
				}
				list.add(user);
			}
			return list == null ? null : list.toArray(new User[list.size()]);
		}
	};

	@Override
	public PagingResult<User> userSearch(UserQuery userQuery, Paging paging) throws Throwable {
		StringBuilder s = new StringBuilder(USER_SQL).append(" WHERE 1=1 ");
		ArrayList<Object> parameters = new ArrayList<>();
		if (userQuery != null) {
			SQLConnectionProvider sqlConnectionProvider = getSQLConnectionProvider();
			if (userQuery.getUserId() > 0) {
				s.append(" AND T6010.F01=?");
				parameters.add(userQuery.getUserId());
			}
			if (!StringHelper.isEmpty(userQuery.getSjh())) {
				s.append(" AND T6010.F04 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(userQuery.getSjh()));
			}
			if (!StringHelper.isEmpty(userQuery.getName())) {
				s.append(" AND T6011.F02 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(userQuery.getName()));
			}
			if (!StringHelper.isEmpty(userQuery.getZjh())) {
				s.append(" AND T6011.F04 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(userQuery.getZjh()));
			}
			if (userQuery.getCreateTimeStart() != null) {
				s.append(" AND DATE(T6011.F09) >= ?");
				parameters.add(userQuery.getCreateTimeStart());
			}
			if (userQuery.getCreateTimeEnd() != null) {
				s.append(" AND DATE(T6011.F09) <= ?");
				parameters.add(userQuery.getCreateTimeEnd());
			}
			if (userQuery.getUserState() != null) {
				s.append(" AND T6010.F06= ?");
				parameters.add(userQuery.getUserState());
			}
			if (userQuery.getSftgjrz() != null) {
				if (userQuery.getSftgjrz() == IsPass.F) {
					s.append(" AND (T6011.F13= ? OR T6011.F13 IS NULL )");
				} else {
					s.append(" AND T6011.F13=?");
				}
				parameters.add(userQuery.getSftgjrz());
			}
		}
		s.append(" ORDER BY T6011.F09 DESC ");
		return selectPaging(getConnection(P2PConst.DB_USER), USER_PARSER, paging, s.toString(), parameters);
	}

	@Override
	public User get(int userid) throws Throwable {
		if (userid <= 0) {
			throw new ParameterException("ID异常。");
		}
		String sql = "SELECT T6010.F01 userid, T6010.F02 username,T6010.F04 iphone,T6010.F06 sfsd,"
				+ "	T6011.F02 name,T6011.F03 zjlx,T6011.F04 zjhm,T6011.F09 zcsj,T6011.F13 sftggjrz,T6010.F12 ts,T6010.F14 zbxe"
				+ " FROM T6010 LEFT JOIN T6011 ON T6010.F01=T6011.F01 WHERE T6010.F01=?";
		return select(getConnection(P2PConst.DB_USER), new ItemParser<User>() {
			User user = new User();

			@Override
			public User parse(ResultSet resultSet) throws SQLException {
				while (resultSet.next()) {
					user.userId = resultSet.getInt(1);
					user.userName = resultSet.getString(2);
					user.iphone = resultSet.getString(3);
					user.userState = EnumParser.parse(IsPass.class, resultSet.getString(4));
					user.name = resultSet.getString(5);
					user.certificateType = EnumParser.parse(CertificateType.class, resultSet.getString(6));
					user.zjhm = resultSet.getString(7);
					user.registrationTime = resultSet.getTimestamp(8);
					user.sftggjrz = EnumParser.parse(IsPass.class, resultSet.getString(9));
					user.tszh = EnumParser.parse(IsPass.class, resultSet.getString(10));
					user.zbxe = resultSet.getInt(11);
				}
				return user;
			}

		}, sql, userid);
	}

	@Override
	public void updateUser(UserQuery query, int userid) throws Throwable {
		if (userid <= 0) {
			throw new ParameterException("ID异常。");
		}
		if (StringHelper.isEmpty(query.getName())) {
			throw new ParameterException("姓名不能为空。");
		}
		int sjh = selectInt(getConnection(P2PConst.DB_USER),
				"SELECT COUNT(F04) FROM T6010 WHERE  F01<>? AND (F02=? OR F04=?)", userid, query.getSjh(),
				query.getSjh());
		if (sjh > 0) {
			throw new ParameterException("手机号已经存在。");
		}
		int zjh = selectInt(getConnection(P2PConst.DB_USER), "SELECT COUNT(F04) FROM T6011 WHERE  F01<>? AND F04=?",
				userid, query.getZjh());
		if (zjh > 0) {
			throw new ParameterException("证件号已经存在。");
		}
		execute(getConnection(P2PConst.DB_USER), "UPDATE T6010 SET F02=?,F04=?,F12=?,F14=? WHERE F01=?", query.getSjh(),
				query.getSjh(), query.getSfts(), query.getZbxe(), userid);
		execute(getConnection(P2PConst.DB_USER), "UPDATE T6011 SET F02=?,F03=?,F04=? WHERE F01=?", query.getName(),
				query.getCertificateType(), query.getZjh(), userid);
	}

	@Override
	public void sdyh(IsPass sfsd, int userid) throws Throwable {
		if (userid <= 0) {
			throw new ParameterException("ID异常。");
		}
		execute(getConnection(P2PConst.DB_USER), "UPDATE T6010 SET F06=? WHERE F01=?", sfsd, userid);
		log(serviceResource.getSession().getAccountId(), "锁定用户ID：" + userid + "->" + sfsd);
	}

	@Override
	public PagingResult<User> gjsmList(UserQuery userQuery, Paging paging) throws Throwable {
		StringBuilder s = new StringBuilder(
				"SELECT T6010.F01,T6010.F04 sjh,T6011.F02 name,T6011.F04 zjhm,T6011.F14 rzsj,T6011.F13 sftggjrz "
						+ " FROM  T6011_1 LEFT JOIN T6011 ON T6011_1.F01=T6011.F01 LEFT JOIN T6010 ON T6010.F01=T6011.F01  WHERE 1=1 ");
		ArrayList<Object> parameters = new ArrayList<>();
		if (userQuery != null) {
			SQLConnectionProvider sqlConnectionProvider = getSQLConnectionProvider();
			if (!StringHelper.isEmpty(userQuery.getSjh())) {
				s.append(" AND T6010.F04 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(userQuery.getSjh()));
			}
			if (!StringHelper.isEmpty(userQuery.getName())) {
				s.append(" AND T6011.F02 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(userQuery.getName()));
			}
			if (!StringHelper.isEmpty(userQuery.getZjh())) {
				s.append(" AND T6011.F04 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(userQuery.getZjh()));
			}
			if (userQuery.getCreateTimeStart() != null) {
				s.append(" AND DATE(T6011.F14) >= ?");
				parameters.add(userQuery.getCreateTimeStart());
			}
			if (userQuery.getCreateTimeEnd() != null) {
				s.append(" AND DATE(T6011.F14) <= ?");
				parameters.add(userQuery.getCreateTimeEnd());
			}
			if (userQuery.getSftgjrz() != null) {
				s.append(" AND T6011.F13=?");
				parameters.add(userQuery.getSftgjrz());
			} else {
				s.append(" AND T6011.F13 IS NULL");
			}
		}
		s.append(" ORDER BY  T6011.F14 DESC ");
		System.out.println(s.toString());
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<User>() {
			ArrayList<User> list = new ArrayList<User>();

			@Override
			public User[] parse(ResultSet resultSet) throws SQLException {
				while (resultSet.next()) {
					User user = new User();
					user.userId = resultSet.getInt(1);
					user.iphone = resultSet.getString(2);
					user.name = resultSet.getString(3);
					user.zjhm = resultSet.getString(4);
					user.rzsj = resultSet.getTimestamp(5);
					user.sftggjrz = EnumParser.parse(IsPass.class, resultSet.getString(6));
					if (list == null) {
						list = new ArrayList<>();
					}
					list.add(user);
				}
				return list == null ? null : list.toArray(new User[list.size()]);
			}
		}, paging, s.toString(), parameters);
	}

	@Override
	public void gjrzSh(int userid, IsPass type, String bz) throws Throwable {
		if (userid <= 0) {
			throw new ParameterException("ID异常。");
		}
		if (type == null) {
			throw new ParameterException("审核状态不能为空。");
		}
		execute(getConnection(P2PConst.DB_USER), "UPDATE T6011 SET F13=?,F14=CURRENT_TIMESTAMP() WHERE F01=?", type,
				userid);
		if (!StringHelper.isEmpty(bz)) {
			execute(getConnection(P2PConst.DB_USER), "UPDATE T6011_1 SET F04=? WHERE F01=?", bz, userid);
		}
		
		if (type.equals(IsPass.S)) {
			present(userid);
			
			//推荐人赠送
			String yqrm = selectString(P2PConst.DB_USER, "SELECT F06 FROM T6011 WHERE F01=?" , userid);
			
			if (null != yqrm && !yqrm.equals("")) {
				int yqrId = selectInt(P2PConst.DB_USER, "SELECT F01 FROM T6011 WHERE F07=?", yqrm);
				if (yqrId != 0) {
					yqrpresent(yqrId);
				}
			}
			
		}
		sms(userid, type);
		log(serviceResource.getSession().getAccountId(), "高级实名认证ID：" + userid + "->" + type);
	}

	/**
	 * 发送短信 2017年8月1日13:47:20
	 * 
	 * @throws IOException
	 */
	private void sms(int userid, IsPass s) throws SQLException, IOException {

		if (userid > 0) {
			ConfigureProvider configureProvider = serviceResource.getResource(ConfigureProvider.class);
			String c = configureProvider.getProperty(SystemVariable.SMS_GJSMRZBH);
			String english = "Dear BTB users, since your uploaded photos do not meet the requirements, the real name authentication has not been adopted. Please re upload as requested. If there is any problem, please contact customer service. <br><br>";
			if (s == IsPass.S) {
				c = configureProvider.getProperty(SystemVariable.SMS_GJSMRZTG);
				english = "Dear BTB user, your real name advanced authentication has been successful. Thank you for using NEWBI.<br><br>";
			}
			String iphone = selectString(P2PConst.DB_USER, "SELECT F02 FROM T6010 WHERE F01=?", userid);// 发送短信
			String content = english + c;
			EmailUtil.sendHtmlMail(iphone, EmailUtil.NIKENAME, content + "<br><br><br><br>", "系统错误", true);
		}
	}

	@Override
	public User getSfz(int userid) throws Throwable {
		if (userid <= 0) {
			throw new ParameterException("ID异常。");
		}
		return select(getConnection(P2PConst.DB_USER), new ItemParser<User>() {
			User user = new User();

			@Override
			public User parse(ResultSet resultSet) throws SQLException {
				while (resultSet.next()) {
					user.sc_zmz = resultSet.getBlob(1);
					user.sc_fmz = resultSet.getBlob(2);
				}
				return user;
			}

		}, "SELECT F02,F03 FROM T6011_1 WHERE F01=?", userid);
	}

	@Override
	public ZhxxEntity rmbZhxx(int userid) throws Throwable {
		if (userid <= 0) {
			throw new ParameterException("ID异常。");
		}
		return select(getConnection(P2PConst.DB_USER), new ItemParser<ZhxxEntity>() {

			@Override
			public ZhxxEntity parse(ResultSet re) throws SQLException {
				ZhxxEntity z = new ZhxxEntity();
				while (re.next()) {
					z.ze = re.getBigDecimal(1);
					z.dj = re.getBigDecimal(2);
					z.ky = re.getBigDecimal(3);
				}

				return z;
			}
		}, "SELECT F02,F03,F04 FROM T6023 WHERE F01=?", userid);
	}

	@Override
	public PagingResult<ZhxxEntity> xlbZhxx(int userid, Paging paging) throws Throwable {
		if (userid <= 0) {
			throw new ParameterException("ID异常。");
		}
		StringBuffer s = new StringBuffer("SELECT T6025.F04 ky,T6025.F05 dj,T6013.F02 jc,T6013.F04 zwm ");
		s.append(" FROM T6025 LEFT JOIN T6013 ON T6025.F03=T6013.F01");
		s.append(" WHERE T6025.F02=?");
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<ZhxxEntity>() {
			ArrayList<ZhxxEntity> list = new ArrayList<ZhxxEntity>();

			@Override
			public ZhxxEntity[] parse(ResultSet re) throws SQLException {
				while (re.next()) {
					ZhxxEntity z = new ZhxxEntity();
					z.ky_zc = re.getBigDecimal(1);
					z.dj_zc = re.getBigDecimal(2);
					z.ywm = re.getString(3);
					z.zem = re.getString(4);
					list.add(z);
				}
				return list == null ? null : list.toArray(new ZhxxEntity[list.size()]);
			}
		}, paging, s.toString(), userid);
	}

	public PagingResult<CzglRecord> getUserRechargeRecordList(CzglRecordQuery query, Paging paging, int userid)
			throws Throwable {
		StringBuilder sql = new StringBuilder(
				"SELECT T6033.F03 cjsj,T6033.F04 je,T6033.F05 zt,T6033.F10 czkh,T6033.F11 lx,T6033.F12 sfzd,T6033.F01,T6033.F13 ");
		sql.append(" FROM T6033 WHERE 1=1");
		ArrayList<Object> parameters = new ArrayList<>();
		if (query != null) {
			SQLConnectionProvider sqlConnectionProvider = getSQLConnectionProvider();
			if (query.getUserid() > 0) {
				sql.append(" AND T6033.F02=?");
				parameters.add(query.getUserid());
			}
			if (!StringHelper.isEmpty(query.getYhhk())) {
				sql.append(" AND T6033.F10 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(query.getYhhk()));
			}

			Timestamp timestamp = query.getStartRechargeTime();
			if (timestamp != null) {
				sql.append(" AND DATE(T6033.F03) >= ?");
				parameters.add(timestamp);
			}
			timestamp = query.getEndRechargeTime();
			if (timestamp != null) {
				sql.append(" AND DATE(T6033.F03) <= ?");
				parameters.add(timestamp);
			}
			if (query.getChargeType() != null) {
				sql.append(" AND T6033.F11 = ?");
				parameters.add(query.getChargeType());
			}
			ChargeStatus status = query.getStatus();
			if (status != null) {
				sql.append(" AND T6033.F05 = ?");
				parameters.add(status.name());
			}
			if (query.getZddz() != null) {
				sql.append(" AND T6033.F12=?");
				parameters.add(query.getZddz());
			}
		}
		sql.append(" AND T6033.F02=?  ORDER BY T6033.F12 DESC,T6033.F03 DESC");
		parameters.add(userid);
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<CzglRecord>() {
			ArrayList<CzglRecord> list = new ArrayList<CzglRecord>();

			public CzglRecord[] parse(ResultSet rst) throws SQLException {
				while (rst.next()) {
					CzglRecord user = new CzglRecord();
					user.chargeDateTime = rst.getTimestamp(1);
					user.userAmount = rst.getBigDecimal(2);
					user.rechargeStatus = EnumParser.parse(ChargeStatus.class, rst.getString(3));
					user.czkh = rst.getString(4);
					user.chargeType = EnumParser.parse(ChargeType.class, rst.getString(5));
					user.is = EnumParser.parse(IsPass.class, rst.getString(6));
					user.id = rst.getInt(7);
					user.tjjeAmount = rst.getBigDecimal(8);
					list.add(user);
				}
				return list.toArray(new CzglRecord[list.size()]);
			}
		}, paging, sql.toString(), parameters);
	}

	public PagingResult<TxglRecord> search(String startRechargeTime, String endRechargeTime, String status,
			Paging paging, int usid) throws Throwable {
		StringBuilder sql = new StringBuilder(
				"SELECT T1.F01, T2.F02, T3.F02 name, T5.F02 AS BANK, T4.F05 khzh, T4.F06 AS F15, T1.F04 AS F16, T1.F06, T1.F08, T1.F12,T1.F07 AS STATUS,T2.F01 AS USERID,T1.F03 AS CREATETIME,T1.F09 FROM T6034 T1");
		sql.append(" LEFT JOIN T6010 T2 ON T1.F02 = T2.F01");
		sql.append(" LEFT JOIN T6011 T3 ON T1.F02 = T3.F01");
		sql.append(" LEFT JOIN T6024 T4 ON T1.F05 = T4.F01");
		sql.append(" LEFT JOIN T6052 T5 ON T4.F03=T5.F01 WHERE 1=1");
		ArrayList<Object> parameters = new ArrayList<>();
		if (usid > 0) {
			sql.append(" AND T1.F02=?");
			parameters.add(usid);
		}
		if (!StringHelper.isEmpty(startRechargeTime)) {
			sql.append(" AND DATE(T1.F03)>=?");
			parameters.add(startRechargeTime);
		}
		if (!StringHelper.isEmpty(endRechargeTime)) {
			sql.append(" AND DATE(T1.F03)<=?");
			parameters.add(endRechargeTime);
		}
		if (!StringHelper.isEmpty(status)) {
			sql.append(" AND T1.F07=?");
			parameters.add(status);
		}
		sql.append(" ORDER BY T1.F03 DESC");

		return selectPaging(getConnection(ConsoleConst.DB_USER), new ArrayParser<TxglRecord>() {
			ArrayList<TxglRecord> list = new ArrayList<TxglRecord>();

			public TxglRecord[] parse(ResultSet rst) throws SQLException {
				while (rst.next()) {
					TxglRecord user = new TxglRecord();
					user.id = rst.getInt(1);
					user.loginName = rst.getString(2);
					user.userName = rst.getString(3);
					user.extractionBank = rst.getString(4);
					user.subbranch = rst.getString(5);
					user.bankId = rst.getString(6);
					user.extractionAmount = rst.getBigDecimal(7);
					user.charge = rst.getBigDecimal(8);
					user.checkDateTime = rst.getTimestamp(9);
					user.applyDateTime = rst.getTimestamp(10);
					user.status = EnumParser.parse(WithdrawStatus.class, rst.getString(11));
					user.userId = rst.getInt(12);
					user.createTime = rst.getTimestamp(13);
					user.remark = rst.getString(14);
					list.add(user);
				}
				return list.toArray(new TxglRecord[list.size()]);
			}
		}, paging, sql.toString(), parameters);
	}

	@Override
	public ZhxxEntity xlbZhxx(int userid, int bid) throws Throwable {
		if (userid <= 0) {
			throw new ParameterException("ID异常。");
		}
		if (bid <= 0) {
			throw new ParameterException("币ID异常。");
		}
		return select(getConnection(P2PConst.DB_USER), new ItemParser<ZhxxEntity>() {

			@Override
			public ZhxxEntity parse(ResultSet re) throws SQLException {
				ZhxxEntity z = new ZhxxEntity();
				while (re.next()) {
					z.ky = re.getBigDecimal(1);
					z.dj = re.getBigDecimal(2);
				}

				return z;
			}
		}, "SELECT F04,F05 FROM T6025 WHERE F02=? AND F03=?", userid, bid);
	}

	@Override
	public void editUserRmb(String userid, BigDecimal num) throws Throwable {

		execute(getConnection(P2PConst.DB_USER),
				"INSERT INTO T6023 SET F01=?,F02=?,F04=? ON DUPLICATE KEY UPDATE F02=?,f03=0,F04=?", userid, num, num,
				num, num);
	}

	@Override
	public void editUserXlb(String userid, int bid, BigDecimal num) throws Throwable {

		execute(getConnection(P2PConst.DB_USER),
				"INSERT INTO T6025 SET F02=?,F03=?,F04=? ON DUPLICATE KEY UPDATE F02=?,F03=?,F04=?", userid, bid, num,
				userid, bid, num);
	}

	@Override
	public PagingResult<User> getYqList(int userid, Paging paging) throws Throwable {
		String yqm = selectString(getConnection(P2PConst.DB_USER), "SELECT F07 FROM T6011 WHERE F01=?", userid);
		StringBuilder s = new StringBuilder(USER_SQL).append(" WHERE 1=1 ");
		s.append(" AND T6011.F06=?");
		return selectPaging(getConnection(P2PConst.DB_USER), USER_PARSER, paging, s.toString(), yqm);
	}
	


	private void present(int userId) throws Throwable {

		String sql = "SELECT F02 FROM _1010 WHERE F01=?";
		
		int bid = selectInt(P2PConst.DB_CONFIG, sql, "SYSTEM.ZSBID");
		
		if (bid <= 0) {
			return;
		}
		
		int count = selectInt(P2PConst.DB_CONFIG, sql, "SYSTEM.ZSSL");
		
		if (count > 0) {
			execute(getConnection(P2PConst.DB_USER),
					"INSERT INTO T6025 SET F02=?,F03=?,F04=? ON DUPLICATE KEY UPDATE F02=?,F03=?,F04=F04+?",
					userId, bid, count, userId, bid, count);
			execute(getConnection(P2PConst.DB_USER),
					"INSERT INTO T6026 SET F02=?,F03=?,F04=?,F05=CURRENT_TIMESTAMP(),F06=?,F07=?,F08=?",
					userId, bid, XlbType.ZCZS, count, userId, "注册赠送");
		}
			
	}
	
	private void yqrpresent(int yqrId) throws Throwable {

		String sql = "SELECT F02 FROM _1010 WHERE F01=?";
		
		int bid = selectInt(P2PConst.DB_CONFIG, sql, "SYSTEM.YQRZSBID");
		
		if (bid <= 0) {
			return;
		}
		
		int count = selectInt(P2PConst.DB_CONFIG, sql, "SYSTEM.YQRZSSL");
		
		if (count > 0) {
			execute(getConnection(P2PConst.DB_USER),
					"INSERT INTO T6025 SET F02=?,F03=?,F04=? ON DUPLICATE KEY UPDATE F02=?,F03=?,F04=F04+?",
					yqrId, bid, count, yqrId, bid, count);
			execute(getConnection(P2PConst.DB_USER),
					"INSERT INTO T6026 SET F02=?,F03=?,F04=?,F05=CURRENT_TIMESTAMP(),F06=?,F07=?,F08=?",
					yqrId, bid, XlbType.YQZS, count, yqrId, "邀请赠送");
		}
			
	}
}
